$(function() {
	FastClick.attach(document.body);
});

var statisticsProjectVue = new Vue({
	el: "body",
	data: {
		lineData: [],
		zongBiaoTatal: [], //总表的全部汇总
		gongYeTotal: [], //工业管线全部汇总
		statrYear: "",
		startWeek: "",
		endYear: "",
		endWeek: "",
		nowPage: 0, //保存当前加载的页数
		loadMore: false, //控制显示加载更多信息
		totalNum: 0, //当前信息总量
		noMore: false, //控制显示已无更多数据信息
		loading: false, //控制在下拉一次未完成之间不再执行下拉
		indexNum: 0
	},
	methods: {
		chaXun: function() {
			_this.indexNum = 1;
			$(".statistics-line-tabbar>div").eq(1).removeClass("forbidden")
			$(".statistics-line-list").addClass("hidden");
			$(".loader").removeClass("hidden");
			searchLine(_this, "查询");
			_this.$nextTick(function() {
				setTimeout(function() {
					$(".statistics-line-list").removeClass("hidden")
					$(".loader").addClass("hidden")
				}, 200)
			})
		},
		ruKu: function() {
			_this.indexNum = 2;
			$(".statistics-line-tabbar>div").eq(1).removeClass("forbidden")
			$(".statistics-line-list").addClass("hidden")
			$(".loader").removeClass("hidden")
			searchLine(_this, "入库")
			_this.$nextTick(function() {
				setTimeout(function() {
					$(".statistics-line-list").removeClass("hidden")
					$(".loader").addClass("hidden")
				}, 200)
			})
		},
		//数据总表
		zongBiao: function() {
			_this.indexNum = 3;
			_this.totalNum = 0
			$(".statistics-line-tabbar>div").eq(1).addClass("forbidden")
			$(".statistics-line-list").addClass("hidden")
			$(".loader").removeClass("hidden")
			searchZongbiaoNow(_this)
			searchZongbiaoSum()
			_this.$nextTick(function() {
				setTimeout(function() {
					$(".statistics-line-list").removeClass("hidden")
					$(".loader").addClass("hidden")
				}, 200)
			})
		},
		gongYe: function() {
			_this.indexNum = 4;
			_this.totalNum = 0
			$(".statistics-line-tabbar>div").eq(1).addClass("forbidden")
			$(".statistics-line-list").addClass("hidden")
			$(".loader").removeClass("hidden")
			searchGongYeNow(_this)
			searchGongYeSum()
			_this.$nextTick(function() {
				setTimeout(function() {
					$(".statistics-line-list").removeClass("hidden")
					$(".loader").addClass("hidden")
				}, 200)
			})
		},
		back: function() {
			window.history.go(-1); //返回上一页
		}
	},
	created: function() {
		_this = this //防止出现闭包

		_this.startYear = getUrlItem("startYear")
		_this.startWeek = getUrlItem("startWeek")
		_this.endYear = getUrlItem("endYear")
		_this.endWeek = getUrlItem("endWeek")
		//		判断当前是搜索页还是本周页

		searchLineChart(_this)

		//判断是否需要下拉刷新
		$(document.body).infinite().on("infinite", function() {
			//一页显示10条数据,因此当列表中数据大于等于10时才可以进行下拉刷新
			var nowSelect = $(".statistics-line-tabbar .active").html(); //判断当前是否在数据图页面，如果在数据图页面，下来刷新不起作用
			if(_this.lineData.length >= 12 && _this.totalNum > 0 && nowSelect == "数据详情") {
				//防止多次进行刷新重复操作
				if(_this.loading) return;
				_this.loading = true;
				_this.loadMore = true;
				var beClick = $(".statistics-line-title .tab .active").html(); //判断当前点击的是下方是下方三个tab的哪一个
				console.log(beClick)
				setTimeout(function() {
					var canPage = Math.ceil(_this.totalNum / 12)
					//	当前页大于分页总页则显示已无更多数据
					switch(beClick) {
						case "查询":
							if(canPage > _this.nowPage) {
								searchMoreLine(_this, "查询")
							} else {
								_this.noMore = true;
								_this.loadMore = false;
								_this.loading = true;
							}
							break;
						case "入库":
							if(canPage > _this.nowPage) {
								searchMoreLine(_this, "入库")
							} else {
								_this.noMore = true;
								_this.loadMore = false;
								_this.loading = true;
							}
							break;
					};
				}, 2000);
			} else {
				_this.noMore = false;
			}
		});

	},
	ready: function() {
		$(".statistics-line-title .tab>div").each(function(i) {
			$(".statistics-line-title .tab>div").eq(i).click(function() {
				$(".statistics-line-title .tab div").removeClass("active")
				$(this).addClass("active")
			})
		})
		//顶部tab切换

		$(".statistics-line-tabbar>div").eq(1).click(function() {
			var beclick = $(".statistics-line-title  .active").html()
			if(beclick != "管线数据" && beclick != "长输工业管线") {
				$(".statistics-line-tabbar>div").removeClass("active")
				$(this).addClass("active")
				$(".statistics-line-body").hide()
				$(".statistics-line-title").addClass("hidden")
				$(".statistics-line-cart").removeClass("hidden")
			} else {

			}
		})

		$(".statistics-line-tabbar>div").eq(0).click(function() {
			$(".statistics-line-tabbar>div").removeClass("active")
			$(this).addClass("active")
			$(".statistics-line-body").show()
			$(".statistics-line-title").removeClass("hidden")
			$(".statistics-line-cart").addClass("hidden")
		})
		//底部tab切换

		$(function() {
			setFullScreen(".statistics-line-cart", 50)
			var type = getUrlItem("type");
			if(type == "chaxun") {
				$(".statistics-line-title .tab>div").eq(0).click()
			}
			if(type == "ruku") {
				$(".statistics-line-title .tab>div").eq(1).click()
			}
			if(type == "zongbiao") {
				$(".statistics-line-title .tab>div").eq(2).click()
			}
			if(type == "gongye") {
				$(".statistics-line-title .tab>div").eq(3).click()
			}
			$(".statistics-line-tabbar>div").eq(0).click()

		})

	}
})

function searchLine(a, b) {
	a.lineData = [] //切换tab前先清空列表数据防止残留数据触发加载更多操作
	a.nowPage = 1;
	a.noMore = false;
	a.loading = false;
	a.loadMore = false;
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataStatic.asmx/getLineList?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek,
			"curcode": b,
			"pagesize": 12,
			"pageno": a.nowPage
		},
		dataType: "json",
		success: function(jsondata) {
			a.totalNum = jsondata.total_count
			a.lineData = jsondata.data
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});

}

function searchMoreLine(a, b) {
	a.nowPage++;
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataStatic.asmx/getLineList?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek,
			"curcode": b,
			"pagesize": 12,
			"pageno": a.nowPage
		},
		dataType: "json",
		success: function(jsondata) {
			console.log(jsondata)
			for(var i = 0; i < jsondata.data.length; i++) {
				a.lineData.push(jsondata.data[i]);
			}
			a.loadMore = false;
			a.loading = false;
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});

}

function getUrlItem(name) { //id
	var url = location.search
	start = url.indexOf(name + "=")
	if(start != -1) {
		start = start + name.length + 1
		end = url.indexOf("&", start)
		if(end == -1) {
			end = url.length
			return unescape(url.substring(start, end))
		}
		return unescape(url.substring(start, end))
	}
	return '';
}

function searchLineChart(a) {
	var x = []; //X轴
	var y = []; //Y轴
	var xtext = []; //X轴TEXT
	var color = ["#FCB954", "#25AA1A", "#F8863A", "#04A9F5"];
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataSum.asmx/getLineSum?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek
		},
		dataType: "json",
		success: function(jsondata) {
			var jsonList = []
			var s = 1;
			for(var item in jsondata) {
				var key = {
					TYPE: item,
					NUM: jsondata[item]
				}
				jsonList.push(key)
			}
			for(var key in jsonList) {
				jsonList[key].y = jsonList[key].NUM; //给Y轴赋值
				xtext[key] = jsonList[key].TYPE; //给X轴TEXT赋值
				jsonList[key].color = color[key];
			}
			//			console.log(jsonList.slice(0,2))
			chart.series[0].setData(jsonList.slice(0, 2)); //数据填充到highcharts上面
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});
	var chart = new Highcharts.Chart({
		chart: {
			renderTo: 'lineChart',
			type: 'column' //显示类型 柱形
		},
		title: {
			text: '' //图表的标题
		},
		//当鼠标经过时的提示设置
		tooltip: {
			headerFormat: '<span style="font-size:11px"><b>类别：<b></span><br>',
			pointFormat: '<span style="color:{point.color}">{point.TYPE}</span><br><b>{point.y:.1f} 个</b><br/>'
		},
		xAxis: {
			categories: xtext
		},
		yAxis: {
			title: {
				text: '', //Y轴的名称
				style: {
					color: '#333333',
					fontWeight: 'bold',
					fontSize: '14px'
				}
			}
		},
		series: [{
			name: "所处环节"
		}],
		plotOptions: { //plotOptions全局设置
			series: { //对series的全局设置                        
				cursor: 'pointer',
				dataLabels: {
					enabled: true, //是否在节点上显示数据
					color: '#333',
					align: 'center',
					style: { fontSize: '13px' }
				}
			}
		},
		credits: {
			enabled: false //隐藏右下角网址
		},
		exporting: {
			enabled: false //隐藏右上角打印
		},
		legend: {
			enabled: false //去掉底部的标题
		}
	});
}

function setFullScreen(Name, Height) {
	$(document).ready(function() { //DOM加载后就执行
		var WindowHeight = $(window).height();
		$(Name).css('height', WindowHeight - Height);
	});
	$(window).resize(function() { //改变窗体大小时     *resize()变化或者改变事件
		var WindowHeight = $(window).height();
		$(Name).css('height', WindowHeight - Height);
	});
	$(window).scroll(function() { //浏览器有滚动条时的操作   scroll()滚动事件
		var WindowHeight = $(window).height();
		$(Name).css('height', WindowHeight - Height);
	});
}

//管线总表接口
function searchZongbiaoNow(a) {
	a.lineData = [] //切换tab前先清空列表数据防止残留数据触发加载更多操作
	a.nowPage = 1;
	a.noMore = false;
	a.loading = false;
	a.loadMore = false;
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataStatic.asmx/getLineList_ZB?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek,
			"pagesize": 12,
			"pageno": 1
		},
		dataType: "json",
		success: function(jsondata) {
			_this.lineData = jsondata.data
			console.log(_this.lineData)
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});
}

function searchZongbiaoSum() {
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataStatic.asmx/getLineList_ZB?callback=?",
		data: {
			"year": 0,
			"week": 0,
			"year2": 0,
			"week2": 0,
			"pagesize": 0,
			"pageno": 0
		},
		dataType: "json",
		success: function(jsondata) {
			console.log(jsondata)
			_this.zongBiaoTatal = jsondata.data[0]
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});
}

//长输工业管线接口
function searchGongYeNow(a) {
	a.lineData = [] //切换tab前先清空列表数据防止残留数据触发加载更多操作
	a.nowPage = 1;
	a.noMore = false;
	a.loading = false;
	a.loadMore = false;
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataStatic.asmx/getLineList_GY?callback=?",
		data: {
			"year": a.startYear,
			"week": a.startWeek,
			"year2": a.endYear,
			"week2": a.endWeek,
			"pagesize": 12,
			"pageno": 1
		},
		dataType: "json",
		success: function(jsondata) {
			_this.lineData = jsondata.data
			console.log(_this.lineData)
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});
}

function searchGongYeSum() {
	$.ajax({
		type: "post",
		url: "http://47.92.116.158:8099/BhGgjAPI/DataStatic.asmx/getLineList_GY?callback=?",
		data: {
			"year": 0,
			"week": 0,
			"year2": 0,
			"week2": 0,
			"pagesize": 0,
			"pageno": 0
		},
		dataType: "json",
		success: function(jsondata) {
			_this.gongYeTotal = jsondata.data[0]
			console.log(_this.gongYeTotal)
		},
		error: function(XMLHttpRequest, textStatus, errorThrown) {}
	});
}